package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.FindCarInfoModel;
import com.czy.owner.ui.information.InformationDetailActivity;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyang on 2017/5/5 0005.
 */

public class FindPageAdapter extends PagerAdapter {
    private List<FindCarInfoModel> list = new ArrayList<>();
    private Context context;

    public FindPageAdapter(Context context, List<FindCarInfoModel> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_find_view_page, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.img_item_find_top);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_item_find_title);
        if(list!=null && list.size()>0){
            final FindCarInfoModel model = list.get(position%list.size());
            tvTitle.setText(model.getSubTitle());
            GlideUtils.loadImage(context, list.get(position%list .size()).getHomePageUrl(), imageView, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_banner, R.mipmap.default_banner);
            container.addView(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent ti = new Intent(context, InformationDetailActivity.class);
                    ti.putExtra("title", model.getTitle());
                    ti.putExtra("content", model.getContent());
                    ti.putExtra("imgUrl", model.getCoverUrl());
                    ti.putExtra("infoId", model.getInfoId());
                    ti.putExtra("isCollect",model.isCollect());
                    ti.putExtra("collectType","carInfo");
//                ti.putExtra("findCarInfoModel", model);
                    context.startActivity(ti);

                }
            });
        }

        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}
